<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="net.sf.json.JSONObject,weaver.conn.RecordSet" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map"%>
<%	
	BaseBean bb= new BaseBean();
	RecordSet rs = new RecordSet();
	Map<String, String> dataMap = new HashMap<String, String>();
	bb.writeLog("====getCOQC====");
	String status = request.getParameter("status");
	String diamert = request.getParameter("diamert");
	String customer = request.getParameter("customer");//客户
	String product = request.getParameter("product");
	diamert = getDiameter(diamert);
	String type = request.getParameter("type");
	product = getProduct(product);
	String change = request.getParameter("change");
	String change1 = request.getParameter("change1");
	String formatname = request.getParameter("formatname");
	//String qzd = customer+diamert+product+"Q";
	String qzd = "";
	String lsh = "";
	String bbh = "";
	String flag = "0";
	String message = "";
	String bm = "";
	if(status.equals("1")){
		bb.writeLog("11111");
		//新建第一个
		qzd = customer+diamert+product+"Q";
		//查询对应流水和版本是否存在
		String sql_ishavels = "select 1 from uf_coqcbm where qzd ='"+qzd+"'";
		bb.writeLog("sql——ishave=="+sql_ishavels);
		rs.execute(sql_ishavels);
		boolean ishave = false;
		if(rs.next()){
			ishave = true;
		}
		bb.writeLog("ishave=="+ishave);
		if(ishave){
			//之前存在流水
			String sql_max = "select max(lsh) as lsh  from uf_coqcbm where qzd = '"+qzd+"'";
			bb.writeLog("sql_max=="+sql_max);
			rs.execute(sql_max);
			String lsh_max = "0";
			if(rs.next()){
				lsh_max = Util.null2String(rs.getString("lsh"));
			}
			bb.writeLog("lsh_max=="+lsh_max);
			if(Integer.parseInt(lsh_max)==99){
				message  = "error!";
				flag = "1";
			}else{
				lsh = String.valueOf(Integer.parseInt(lsh_max)+1);
				if(lsh.length()==1){
					lsh = "0"+lsh;
				}
				bbh = "00";
				bm = qzd+lsh+bbh;
				message = "SUCCESS!";
			}
			
		}else{
			lsh = "00";
			bbh = "00";
			bm = qzd+lsh+bbh;
			message = "SUCCESS!";
		}
		bb.writeLog("bbh=="+bbh);
	}else if(status.equals("2")){
		//新建第二个
		String first = formatname.substring(0,6);
		String between = formatname.substring(6,8);
		String last = formatname.substring(8,10);
		if(Integer.parseInt(between)==99){
			message  = "error!";
			flag = "1";
		}else{
			lsh = String.valueOf(Integer.parseInt(between)+1);
			bb.writeLog("lsh=="+lsh);
			if(lsh.length()==1){
				lsh = "0"+lsh;
			}
			bbh = last;
			qzd = first;
			bm = qzd+lsh+bbh;
			message = "SUCCESS!";
		}
		bb.writeLog("bbh=="+bbh);
	}else if(status.equals("3")){
		//变更第一个
		bb.writeLog("3333333");
		String first = change.substring(0,6);
		String between = change.substring(6,8);
		String last = change.substring(8,10);
		bb.writeLog("first=="+first);
		bb.writeLog("between=="+between);
		bb.writeLog("last=="+last);
		String sql_bbh = "select max(bbh) as bbh from uf_coqcbm where qzd='"+first+"' and lsh='"+between+"'";
		String bbh_max = "0";
		bb.writeLog("sql_bbh=="+sql_bbh);
		rs.execute(sql_bbh);
		if(rs.next()){
			bbh_max = Util.null2String(rs.getString("bbh"));
		}
		bb.writeLog("bbh_max=="+bbh_max);
		if(Integer.parseInt(bbh_max)==99){
			message  = "error!";
			flag = "1";
		}else{
			String new_bbh = String.valueOf(Integer.parseInt(bbh_max)+1);
			bb.writeLog("new_bbh=="+new_bbh);
			if(new_bbh.length()==1){
				new_bbh = "0"+new_bbh;
			}
			lsh  = between;
			bbh = new_bbh;
			qzd = first;
			bm = qzd+lsh+bbh;
			message = "SUCCESS!";
		}
	}else if(status.equals("4")){
		//变更第2个
		bb.writeLog("4444444");
		String first = change1.substring(0,6);
		String between = change1.substring(6,8);
		String last = change1.substring(8,10);
		bb.writeLog("first=="+first);
		bb.writeLog("between=="+between);
		bb.writeLog("last=="+last);
		String sql_bbh = "select max(bbh) as bbh from uf_coqcbm where qzd='"+first+"' and lsh='"+between+"'";
		String bbh_max = "0";
		bb.writeLog("sql_bbh=="+sql_bbh);
		rs.execute(sql_bbh);
		if(rs.next()){
			bbh_max = Util.null2String(rs.getString("bbh"));
		}
		bb.writeLog("bbh_max=="+bbh_max);
		if(Integer.parseInt(bbh_max)==99){
			message  = "error!";
			flag = "1";
		}else{
			String new_bbh = String.valueOf(Integer.parseInt(bbh_max)+1);
			bb.writeLog("new_bbh=="+new_bbh);
			if(new_bbh.length()==1){
				new_bbh = "0"+new_bbh;
			}
			lsh  = between;
			bbh = new_bbh;
			qzd = first;
			bm = qzd+lsh+bbh;
			message = "SUCCESS!";
		}
		bb.writeLog("bbh=="+bbh);
	}
	dataMap.put("qzd",qzd);
	dataMap.put("bm",bm);
	dataMap.put("bbh",bbh);
	dataMap.put("lsh",lsh);
	dataMap.put("message",message);
	dataMap.put("flag",flag);
	bb.writeLog("dataMap=="+dataMap);
	JSONObject json = JSONObject.fromObject(dataMap);
	out.print(json.toString());
%>
<%! 
public String getDiameter(String str){
	String ret = "";
	RecordSet rs = new RecordSet();
	String sql = "select dyz from uf_dmtdzb where diameter='"+str+"'";
	rs.execute(sql);
	if(rs.next()){
		ret = Util.null2String(rs.getString("dyz"));
	}
	return ret;
}
public String getProduct(String str){
	String ret = "";
	RecordSet rs = new RecordSet();
	String sql = "select dyz from uf_prodzb where producttype='"+str+"'";
	rs.execute(sql);
	if(rs.next()){
		ret = Util.null2String(rs.getString("dyz"));
	}
	return ret;
}

public String getCoqc(String str){
	String ret = "";
	RecordSet rs = new RecordSet();
	String sql = "select coqcformatname from uf_coqc where id='"+str+"'";
	rs.execute(sql);
	if(rs.next()){
		ret = Util.null2String(rs.getString("coqcformatname"));
	}
	return ret;
}
%>

	